package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.ClassChange;
import com.woniu.pojo.PrivateCourses;
import com.woniu.service.IClassChangeService;
import com.woniu.service.IPrivateCoursesService;
import com.woniu.vo.PrivateCourseQueryVo;
import com.woniu.vo.PrivateCoursesVo;
import com.woniu.vo.privateCoursesAddVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohuang
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/privateCourses")
@Api(tags = "私人课程管理")
@Slf4j
public class PrivateCoursesController extends BaseController{
    @Autowired
    private IPrivateCoursesService privateCoursesService;
    @Autowired
    private IClassChangeService classChangeService;

    @PostMapping("/page/{currentPage}/{pageSize}")
    @ApiOperation("条件查询分页")
    public Result findByPage(@RequestBody(required = false) PrivateCourseQueryVo courseQueryVo,
                             @PathVariable Integer currentPage,
                             @PathVariable Integer pageSize){
        System.out.println(courseQueryVo);
        PageResult pageResult = privateCoursesService.search(courseQueryVo,currentPage,pageSize);
        return toDataResult(pageResult);
    }

    @ApiOperation("通过id查询")
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        PrivateCourses privateCourses = privateCoursesService.getById(id);
        ClassChange classChange = classChangeService.getById(id);
        privateCoursesAddVo privateCoursesAddVo = new privateCoursesAddVo(classChange,privateCourses);
        return toDataResult(privateCoursesAddVo);
    }


    @ApiOperation("添加课程")
    @PostMapping
    public Result saveCourses(@RequestBody privateCoursesAddVo privateCoursesAddVo){
        ClassChange classChange = privateCoursesAddVo.getClassChange();
        PrivateCourses courses = privateCoursesAddVo.getCourses();
        boolean flag = privateCoursesService.save(courses);
        classChange.setId(courses.getId());
        boolean classChangeFlag = classChangeService.save(classChange);
        if (flag && classChangeFlag){
            return toResult(true);
        }else {
            return toResult(false);
        }

    }

    @ApiOperation("修改课程")
    @PutMapping
    public Result modify(@RequestBody privateCoursesAddVo privateCoursesAddVo){
        ClassChange classChange = privateCoursesAddVo.getClassChange();
        PrivateCourses courses = privateCoursesAddVo.getCourses();
        classChange.setId(courses.getId());
        boolean flag = privateCoursesService.updateById(courses);
        boolean b = classChangeService.updateById(classChange);
        return toResult(flag);
    }

    @ApiOperation("删除课程")
    @DeleteMapping("/{id}")
    private Result deleteById(@PathVariable String id){
        Integer row = privateCoursesService.deleteById(id);
        boolean b = classChangeService.removeById(id);
        return toResult(row);
    }


}
